<?php


namespace app\admin\controller;


use app\common\controller\AdminController;
use app\common\model\Admin as adminModel;
use app\common\model\AdminRole as roleModel;
use app\common\model\AdminRoleRel;
use think\facade\Db;
use think\facade\View;
class Admin extends AdminController {


    public function add()
    {
        $result = [
            'status' => false,
            'msg'    => error_code(10019, true),
            'data'   => ''
        ];
        View::engine()->layout(false);
        $adminModel     = new adminModel();
        $roleModel = new roleModel();
        $roleList  = $roleModel->select();
        if ($this->request->isPost()) {
            if (!input('?param.admin_name') || input('param.admin_name') == "" || strlen(input('param.admin_name')) < 6 || strlen(input('param.admin_name')) > 20) {
                return error_code(11008);
            }
            if (!input('?param.mobile') || input('param.mobile') == "") {
                return error_code(11080);
            }
            if (!input('?param.admin_password') || strlen(input('param.admin_password')) < 6 || strlen(input('param.admin_password')) > 16) {
                return error_code(11009);
            }
            $res = $adminModel->toAdd(input('param.'));
            if($res['status']){
                $this->setActionLog('添加管理员',$this->admin_info['name'].'添加了新管理员：'.input('?param.name'));
            }
            return  $res;
        }
        View::assign('roleList', $roleList);
        View::assign('admin_info', '');
        $result['status'] = true;
        $result['msg']    = '成功';
        $result['data']   = View::fetch();
        return $result;
    }



    public function index(){
        if($this->request->isAjax()){
//            $model = new adminModel();
            $re['code'] = 0;
            $re['msg'] = '';
            $re['data'] = [];
            $re['count'] = 0;
            $res = Db::name('admin')
                ->field('a.*,group_concat(ar.name) as role_name')
                ->alias('a')
                ->leftJoin('admin_role_rel arr', 'arr.admin_id = a.admin_id')
                ->leftJoin('admin_role ar', 'ar.id = arr.role_id')
                ->where('a.admin_id', '<>', adminModel::SUPER_ID)
                ->group("a.admin_id")
                ->paginate()->toArray();
            if(sizeof($res['data'])>0){
                $re['data'] = $res['data'];
                $re['count'] = $res['total'];
            }
            return $re;

        }else{
            return View::fetch();

        }
    }




    public function information()
    {
        $adminModel = new adminModel();
        $admin_info  = $adminModel->where(['admin_id' => $this->admin_id])->find();
        View::assign('admin_info', $admin_info);
        return View::fetch();
    }

    /**
     * 编辑管理员
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $result = [
            'status' => false,
            'msg'    => error_code(10021, true),
            'data'   => ''
        ];
        View::engine()->layout(false);
        if (!input('?param.id')) {
            return error_code(10000);
        }

        $adminModel = new adminModel();
        if (input('param.id') == $adminModel::SUPER_ID) {
            return error_code(11023);
        }
        $manageInfo = $adminModel->where(['admin_id' => input('param.id')])->find();
        if (!$manageInfo) {
            return error_code(11004);
        }

        if ($this->request->isPost()) {
            $res = $adminModel->toAdd(input('param.'));
            if($res['status']){
                $this->setActionLog('编辑管理员',$this->admin_info['name'].'编辑了新管理员：'.input('?param.name').json_encode(input('?param')));
            }
            return $res;
        }

        $roleModel    = new roleModel();
        $roleList     = $roleModel->select();
        $roleRelModel = new AdminRoleRel();
        $smList             = $roleRelModel->where(['admin_id' => input('param.id')])->select();
        foreach ($roleList as $k => $v) {
            $checked = false;
            foreach ($smList as $i => $j) {
                if ($j['role_id'] == $v['id']) {
                    $checked = true;
                    break;
                }
            }
            $roleList[$k]['checked'] = $checked;
        }
        View::assign('roleList', $roleList);
        View::assign('admin_info', $manageInfo);
        $result['status'] = true;
        $result['msg']    = '成功';
        $result['data']   = View::fetch();
        return $result;
    }


    public function editPwd(){

        if (!('?param.newPwd') || !input('?param.password') || !input('?param.rePwd')) {
            return error_code(11009);
        }
        if (input('param.newPwd') != input('param.rePwd')) {
            return  error_code(11025);
        }
        $adminModel = new adminModel();
        $res = $adminModel->changePwd($this->admin_id, input('param.password'), input('param.newPwd'));

        if($res['status']){
            $this->setActionLog('修改密码',$this->admin_info['name']);
        }
        return $res;
    }

    /**
     * 删除管理员
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        $result = [
            'status' => false,
            'msg'    => error_code(10023, true),
            'data'   => ''
        ];
        if (!input('?param.id')) {
            return error_code(10000);
        }

        $manageModel = new adminModel();
        if (input('param.id') == $manageModel::TYPE_SUPER_ID) {
            return error_code(11024);
        }

        $where['admin_id'] = input('param.id');
        $re          = $manageModel->where($where)->delete();
        if ($re) {
            $result['status'] = true;
            $result['msg']    = '删除成功';
        } else {
            $result['msg'] = error_code(10023, true);
        }

        return $result;
    }


}